﻿using Quartz;
using SX.Core;
using SX.Data;
using SX.Utils;
using SX.Utils.Log.log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ly.Scheduler.Common
{
    /// <summary>
    /// 同步整车信息
    /// </summary>
    [DisallowConcurrentExecution]
    public class SyncProductJob : IJob
    {
        private readonly ILog logger = LogManager.GetLogger(typeof(SyncProductJob));
        public void Execute(IJobExecutionContext context)
        {
            logger.Info("开始同步整车信息");
            var lydb = Db.Context("LYDB");
            var lyNewdb = Db.Context("LYNew");
            List<dynamic>  dataList = lydb.Sql("SELECT  DISTINCT a.ProductCode,a.PrefixName  FROM Product(NOLOCK) a WHERE a.ProductCode LIKE '_____00__'  AND a.Discontinued =0").QueryMany<dynamic>();
            foreach (dynamic data in dataList)
            {
                string ProductCode = CommonHelper.ToString(data.ProductCode);
                string PrefixName = CommonHelper.ToString(data.PrefixName);  
                lyNewdb.Sql(@"insert into wh_product_info(product_code,product_prefixname,whpi_status,create_id,update_id)
                    values(@product_code,@product_prefixname,@whpi_status,1,1) ON CONFLICT(product_code) 
                    do update 
                    set product_code=EXCLUDED.product_code,
                    product_prefixname=EXCLUDED.product_prefixname ,
                    whpi_status=EXCLUDED.whpi_status, 
                    update_time=now();  ")
                .Parameter("product_code", ProductCode, DataTypes.String) 
                .Parameter("product_prefixname", PrefixName, DataTypes.String) 
                .Parameter("whpi_status", 0, DataTypes.Int32)  
                .Execute();
            } 
            logger.Info("同步整车信息完成");
        } 
    }
}
